import Qt 4.7

Item {
    // api variables: these are updated to represent the selected item
    property int selectedDate
    property int selectedMonth
    property int selectedYear

    // internal variables, not updated after initial load completed
    property int previousDaysToPopulate: 31
    property int nextDaysToPopulate: 0
    property int todayIndex: previousDaysToPopulate

    id: daySelector
    width: screenWidth

    function populate() {
        var currentDate = new Date();
        var dayInMsecs = 1000 * 60 * 60 * 24;
        var tmpDay = new Date();

        // populate previous days
        for (var i = previousDaysToPopulate; i > 0; i--) {
            tmpDay.setTime(currentDate.getTime() - dayInMsecs * i);
            dateModel.append({
                "date": tmpDay.getDate(),
                "month": tmpDay.getMonth() + 1,
                "year": tmpDay.getFullYear()
            });
        }

        // populate next days
        for (var i = 0; i < nextDaysToPopulate + 1; i++) {
            tmpDay.setTime(currentDate.getTime() + dayInMsecs * i);
            dateModel.append( {
                "date": tmpDay.getDate(),
                "month": tmpDay.getMonth() + 1,
                "year": tmpDay.getFullYear()
            });
        }

        selectedDate = currentDate.getDate();
        selectedMonth = currentDate.getMonth();
        selectedYear = currentDate.getFullYear();
    }

    function setApiVariables(dateModelIndex) {
        selectedDate = dateModel.get(dateModelIndex).date
        selectedMonth = dateModel.get(dateModelIndex).month
        selectedYear = dateModel.get(dateModelIndex).year

        console.debug("selectedDate = " + selectedDate)
        console.debug("selectedMonth = " + selectedMonth)
        console.debug("selectedYear = " + selectedYear)
    }

    Component {
        id: dateSelectorComponent

        Item {
            property bool today: index == todayIndex
            property bool current: ListView.isCurrentItem

            height: 64
            width: 64
            anchors.margins: 4

            MouseArea {
                anchors.fill: parent
                z: 64
                onClicked: dateListView.currentIndex = index
            }

            Text {
                id: textItem
                font.pixelSize: applicationFontSize - 4
                text: today ? qsTr("Today") : model.date + "." + model.month + "."
                color: today ? heiaYellow : heiaLightGrey
                anchors.horizontalCenter: parent.horizontalCenter
            }
            Rectangle {
                anchors.bottom: parent.bottom
                anchors.horizontalCenter: parent.horizontalCenter
                height: 32
                width: 32
                radius: 16
                smooth: true
                color: current ? heiaFocusColorOuter : heiaLightGrey

                Rectangle {
                    anchors.horizontalCenter: parent.horizontalCenter
                    anchors.verticalCenter: parent.verticalCenter
                    height: parent.height - heiaBorderWidth
                    width: parent.width - heiaBorderWidth
                    radius: 16
                    smooth: true
                    color: current ? heiaFocusColorInner : heiaDarkGrey
                }
            }
        }
    }

    ListModel {
        id: dateModel
    }

    ListView {
        id: dateListView
        anchors.fill: parent
        model: dateModel
        delegate: dateSelectorComponent
        onCurrentIndexChanged: setApiVariables(currentIndex)
        orientation: ListView.Horizontal

        Component.onCompleted: {
            populate()
            dateListView.currentIndex = todayIndex
            positionViewAtIndex(todayIndex, ListView.Center);
        }
    }
}
